<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Android基础--多线程断点续传下载 | 不忘初心</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="导语  多线程断点续传下载 介绍">
<meta property="og:type" content="article">
<meta property="og:title" content="Android基础--多线程断点续传下载">
<meta property="og:url" content="http://example.com/2014/10/13/%E5%9F%BA%E7%A1%80/%E5%9F%BA%E7%A1%80--%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0%E4%B8%8B%E8%BD%BD/index.html">
<meta property="og:site_name" content="不忘初心">
<meta property="og:description" content="导语  多线程断点续传下载 介绍">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2014-10-13T01:44:28.000Z">
<meta property="article:modified_time" content="2017-07-10T06:57:57.000Z">
<meta property="article:author" content="wxyass">
<meta property="article:tag" content="Android基础">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="不忘初心" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">不忘初心</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-基础/基础--多线程断点续传下载" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2014/10/13/%E5%9F%BA%E7%A1%80/%E5%9F%BA%E7%A1%80--%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0%E4%B8%8B%E8%BD%BD/" class="article-date">
  <time class="dt-published" datetime="2014-10-13T01:44:28.000Z" itemprop="datePublished">2014-10-13</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android%E5%9F%BA%E7%A1%80/">Android基础</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Android基础--多线程断点续传下载
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h4 id="导语"><a href="#导语" class="headerlink" title="导语"></a>导语</h4><blockquote>
<p> 多线程断点续传下载 介绍  </p>
</blockquote>
<span id="more"></span> 


<h1 id="一、HttpClient"><a href="#一、HttpClient" class="headerlink" title="一、HttpClient"></a>一、HttpClient</h1><h3 id="1-1-发送get请求"><a href="#1-1-发送get请求" class="headerlink" title="1.1 发送get请求"></a>1.1 发送get请求</h3><ul>
<li><p>创建一个客户端对象</p>
<p>  HttpClient client = new DefaultHttpClient();</p>
</li>
<li><p>创建一个get请求对象</p>
<p>  HttpGet hg = new HttpGet(path);</p>
</li>
<li><p>发送get请求，建立连接，返回响应头对象</p>
<p>  HttpResponse hr = hc.execute(hg);</p>
</li>
<li><p>获取状态行对象，获取状态码，如果为200则说明请求成功</p>
<pre><code>  if(hr.getStatusLine().getStatusCode() == 200)&#123;
      //拿到服务器返回的输入流
      InputStream is = hr.getEntity().getContent();
      String text = Utils.getTextFromStream(is);
  &#125;
</code></pre>
</li>
<li><p>HttpClient框架GET提交用户名密码示例</p>
<pre><code>  //点击事件：HttpClient框架GET提交用户名密码
  public void click1(View v)&#123;
      //获取用户输入的用户名密码
      String name = et_name.getText().toString();
      String pass = et_pass.getText().toString();
      
  //设置提交网址,需要拼接字符串         URLEncoder.encode(name) 此代码将中文进行URL编码，转成二进制(因为地址栏中，纯中文提交参数，接收后会显示乱码,注意接收参数时使用iso-8859-1解码)

      final String address = &quot;http://192.168.15.35:8080/Android_login/userLogin?name=&quot;+URLEncoder.encode(name)+&quot;&amp;pwd=&quot;+pass;
      
      //子线程中，使用HttpClient框架GET方式提交数据
      Thread t = new Thread()&#123;
          @Override
          public void run() &#123;
              //1.创建httpclient对象
              HttpClient client = new DefaultHttpClient();
              //2.创建get请求对象
              HttpGet get = new HttpGet(address);
              try &#123;
                  
                  //3.使用客户端发送get请求，拿到服务器返回的信息
                  HttpResponse response = client.execute(get);
                  //获取响应状态行
                  StatusLine sl =response.getStatusLine();
                  //获取响应状态码，进行判断
                  if (sl.getStatusCode()==200) &#123;
                      //获取响应头输入流实例HttpEntity
                      HttpEntity entity = response.getEntity();
                      //获取流里的数据(字符串)
                      InputStream is = entity.getContent();
                      //将流里的数据转换成字符串
                      String text = Tools.getTextFromStream(is);
                      //4发送消息到消息队列，携带流数据，主界面刷新UI
                      Message message = handler.obtainMessage();
                      message.obj=text;
                      message.what=0;
                      handler.sendMessage(message);
                  &#125;
              &#125; catch (Exception e) &#123;
                  e.printStackTrace();
              &#125;
          &#125;
      &#125;;
      t.start();
  &#125;
</code></pre>
</li>
</ul>
<p>###1.2 发送post请求</p>
<pre><code>    //创建一个客户端对象
    HttpClient client = new DefaultHttpClient();
    //创建一个post请求对象
    HttpPost hp = new HttpPost(path);
</code></pre>
<ul>
<li><p>往post对象里放入要提交给服务器的数据</p>
<pre><code>  //要提交的数据以键值对的形式存在BasicNameValuePair对象中
  List&lt;NameValuePair&gt; parameters = new ArrayList&lt;NameValuePair&gt;();
  BasicNameValuePair bnvp = new BasicNameValuePair(&quot;name&quot;, name);
  BasicNameValuePair bnvp2 = new BasicNameValuePair(&quot;pass&quot;, pass);
  parameters.add(bnvp);
  parameters.add(bnvp2);
  //创建实体对象，指定进行URL编码的码表
  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, &quot;utf-8&quot;);
  //为post请求设置实体
  hp.setEntity(entity);
</code></pre>
</li>
<li><p>HttpClient框架POST提交用户名密码示例</p>
<pre><code>  //点击事件：HttpClient框架POST提交用户名密码
  public void click2(View v)&#123;
      //获取用户输入的用户名密码
      final String name = et_name.getText().toString();
      final String pass = et_pass.getText().toString();
      
      //设置提交网址,不需要拼接字符串
      final String address = &quot;http://192.168.15.35:8080/Android_login/userLogin&quot;;
      
      //子线程中，使用HttpClient框架POST方式提交数据
      Thread t = new Thread()&#123;
          @Override
          public void run() &#123;
              //1.创建httpclient对象
              HttpClient client = new DefaultHttpClient();
              //2.创建post请求对象
              HttpPost post = new HttpPost(address);
              
              //把要提交的数据封装至post对象中
              //把要提交的数据存放至list中
              List&lt;NameValuePair&gt;  parameters = new ArrayList&lt;NameValuePair&gt;();
              NameValuePair nvp1 = new BasicNameValuePair(&quot;name&quot;, name);
              NameValuePair nvp2 = new BasicNameValuePair(&quot;pwd&quot;, pass);
              parameters.add(nvp1);
              parameters.add(nvp2);
              try &#123;
                  //创建实体对象，指定进行URL编码的码表
                  HttpEntity entity = new UrlEncodedFormEntity(parameters,&quot;UTF-8&quot;);
                  //把数据设置到请求头的流中 //为post请求设置实体
                  post.setEntity(entity);
                  //3.使用客户端发送post请求，拿到服务器返回的信息
                  HttpResponse response = client.execute(post);
                  //获取响应状态行
                  StatusLine sl = response.getStatusLine();
                  //获取响应状态码，进行判断
                  if (sl.getStatusCode()==200) &#123;
                      
                      //获取响应头输入流实例HttpEntity
                      HttpEntity entity1 = response.getEntity();
                      //获取流里的数据(字符串)
                      InputStream is = entity1.getContent();
                      //将流里的数据转换成字符串
                      String text = Tools.getTextFromStream(is);
                      //4发送消息到消息队列，携带流数据，主界面刷新UI
                      Message message = handler.obtainMessage();
                      message.obj=text;
                      message.what=0;
                      handler.sendMessage(message);
                  &#125;
              &#125; catch (Exception e) &#123;
                  e.printStackTrace();
              &#125;
          &#125;
      &#125;;
      t.start();
  &#125;
</code></pre>
<p>#二、异步HttpClient框架<br>###2.1 发送get请求</p>
<pre><code>  //创建异步的httpclient对象
  AsyncHttpClient ahc = new AsyncHttpClient();
  //发送get请求
  ahc.get(path, new MyHandler());
</code></pre>
</li>
<li><p>注意AsyncHttpResponseHandler两个方法的调用时机</p>
<p>   class MyHandler extends AsyncHttpResponseHandler{</p>
<pre><code>      //http请求成功，返回码为200，系统回调此方法
      @Override
      public void onSuccess(int statusCode, Header[] headers,
              //responseBody的内容就是服务器返回的数据
              byte[] responseBody) &#123;
          Toast.makeText(MainActivity.this, new String(responseBody), 0).show();
          
      &#125;

      //http请求失败，返回码不为200，系统回调此方法
      @Override
      public void onFailure(int statusCode, Header[] headers,
              byte[] responseBody, Throwable error) &#123;
          Toast.makeText(MainActivity.this, &quot;返回码不为200&quot;, 0).show();
          
      &#125;
  &#125;
</code></pre>
</li>
</ul>
<ul>
<li><p>异步HttpClient框架发送get请求示例</p>
<pre><code> public class MainActivity extends Activity &#123;

      //查找控件
      EditText et_name ;
      EditText et_pass ;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) &#123;
          super.onCreate(savedInstanceState);
          //显示当前页面布局文件
          setContentView(R.layout.activity_main);
          //初始化控件
          et_name = (EditText) findViewById(R.id.et_name);
          et_pass = (EditText) findViewById(R.id.et_pass);
      &#125;
      
      //点击事件：异步HttpClient框架get提交
      public void click1(View v)&#123;
          //获取用户输入的用户名密码
          String name = et_name.getText().toString();
          String pass = et_pass.getText().toString();
          
          //定义表单提交地址
          String address = &quot;http://192.168.15.35:8080/Android_login/userLogin&quot;;
          //创建异步HttpClient对象
          AsyncHttpClient client = new AsyncHttpClient();
          //封装请求参数，封装到框架提供的对象RequestParams
          RequestParams rp = new RequestParams();
          rp.add(&quot;name&quot;, name);//第一个&quot;name&quot; 表单中用户名name属性值，第二个name是获取到的用户输入的用户名
          rp.add(&quot;pwd&quot;, pass);//pwd是表单中密码的name属性值，pass是获取到的用户输入的密码
          //发送get请求(3个参数，1：地址;2:封装请求参数的对象;3:响应处理对象)
          client.get(address,rp,new MyResponseHandler());
          
      &#125;
      //我的响应处理器类
      class MyResponseHandler extends AsyncHttpResponseHandler&#123;
  
          //请求成功时此方法调用，响应码是200开头
          @Override
          public void onSuccess(int statusCode, Header[] headers,
                  byte[] responseBody) &#123;//第三个参数：就是返回的输入流，框架将他转换成字节数组了
              String text = new String(responseBody);
              Toast.makeText(MainActivity.this, text, 0).show();
              
          &#125;
  
          //请求失败时此方法调用
          @Override
          public void onFailure(int statusCode, Header[] headers,
                  byte[] responseBody, Throwable error) &#123;//第三个参数：就是返回的输入流，框架将他转换成字节数组了
              String text = new String(responseBody);
              Toast.makeText(MainActivity.this, text, 0).show();
          &#125;
      &#125;
  &#125;
</code></pre>
</li>
</ul>
<p>###2.2 发送post请求</p>
<ul>
<li><p>使用RequestParams对象封装要携带的数据</p>
<pre><code>  //创建异步httpclient对象
  AsyncHttpClient ahc = new AsyncHttpClient();
  //创建RequestParams封装要携带的数据
  RequestParams rp = new RequestParams();
  rp.add(&quot;name&quot;, name);
  rp.add(&quot;pass&quot;, pass);
  //发送post请求
  ahc.post(path, rp, new MyHandler());
</code></pre>
</li>
</ul>
<ul>
<li><p>异步HttpClient框架发送post请求</p>
<pre><code> public class MainActivity extends Activity &#123;

      //查找控件
      EditText et_name ;
      EditText et_pass ;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) &#123;
          super.onCreate(savedInstanceState);
          //显示当前页面布局文件
          setContentView(R.layout.activity_main);
          //初始化控件
          et_name = (EditText) findViewById(R.id.et_name);
          et_pass = (EditText) findViewById(R.id.et_pass);
      &#125;
      //点击事件：异步HttpClient框架post提交
      public void click2(View v)&#123;
          //获取用户输入的用户名密码
          String name = et_name.getText().toString();
          String pass = et_pass.getText().toString();
          
          //定义表单提交地址
          String address = &quot;http://192.168.15.35:8080/Android_login/userLogin&quot;;
          //创建异步HttpClient对象
          AsyncHttpClient client = new AsyncHttpClient();
          //封装请求参数，封装到框架提供的对象RequestParams
          RequestParams rp = new RequestParams();
          rp.add(&quot;name&quot;, name);//第一个&quot;name&quot; 表单中用户名name属性值，第二个name是获取到的用户输入的用户名
          rp.add(&quot;pwd&quot;, pass);//pwd是表单中密码的name属性值，pass是获取到的用户输入的密码
          //发送get请求(3个参数，1：地址;2:封装请求参数的对象;3:响应处理对象)
          client.post(address,rp,new MyResponseHandler());
      &#125;
      
      //自定义我的响应处理器类
      class MyResponseHandler extends AsyncHttpResponseHandler&#123;
  
          //请求成功时此方法调用，响应码是200开头
          @Override
          public void onSuccess(int statusCode, Header[] headers,
                  byte[] responseBody) &#123;//第三个参数：就是返回的输入流，框架将他转换成字节数组了
              String text = new String(responseBody);
              Toast.makeText(MainActivity.this, text, 0).show();
              
          &#125;
  
          //请求失败时此方法调用
          @Override
          public void onFailure(int statusCode, Header[] headers,
                  byte[] responseBody, Throwable error) &#123;//第三个参数：就是返回的输入流，框架将他转换成字节数组了
              String text = new String(responseBody);
              Toast.makeText(MainActivity.this, text, 0).show();
              
          &#125;
          
      &#125;
  &#125;
</code></pre>
</li>
</ul>
<hr>
<p>#三、多线程下载</p>
<blockquote>
<p>原理：服务器CPU分配给每条线程的时间片相同，服务器带宽平均分配给每条线程，所以客户端开启的线程越多，就能抢占到更多的服务器资源</p>
</blockquote>
<ul>
<li>单线程下载：客户端请求服务器，请求成功拿到输入流，流里就是要下载的电影，客户端只需从流的第0个字节，读取到最后一个字节，并把所有读取的字节写入文件输出流，下载就完成</li>
</ul>
<p>###3.1 确定每条线程下载多少数据</p>
<ul>
<li><p>发送http请求至下载地址</p>
<pre><code>  String path = &quot;http://192.168.1.102:8080/editplus.exe&quot;;        
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setReadTimeout(5000);
  conn.setConnectTimeout(5000);
  conn.setRequestMethod(&quot;GET&quot;);                    
</code></pre>
</li>
<li><p>获取文件总长度，然后创建长度一致的临时文件</p>
</li>
<li><p>硬盘缓冲区：数据读写时，先进入缓冲区，等攒到缓冲区数据满了，再一次性写入硬盘</p>
<pre><code>  if(conn.getResponseCode() == 200)&#123;
      //获得服务器流中数据的长度
      int length = conn.getContentLength();
      //创建一个临时文件存储下载的数据
      RandomAccessFile raf = new RandomAccessFile(getFileName(path), &quot;rwd&quot;);
      //设置临时文件的大小
      raf.setLength(length);
      raf.close();
</code></pre>
</li>
<li><p>确定线程下载多少数据</p>
<pre><code>      //计算每个线程下载多少数据
      int blockSize = length / THREAD_COUNT;
</code></pre>
<p>###3.2 计算每条线程下载数据的开始位置和结束位置</p>
<pre><code>  for(int id = 1; id &lt;= 3; id++)&#123;
      //计算每个线程下载数据的开始位置和结束位置
      int startIndex = (id - 1) * blockSize;
      int endIndex = id * blockSize - 1;
      if(id == THREAD_COUNT)&#123;
          endIndex = length;
      &#125;
                      
      //开启线程，按照计算出来的开始结束位置开始下载数据
      new DownLoadThread(startIndex, endIndex, id).start();
  &#125;
</code></pre>
<p>###3.3 再次发送请求至下载地址，请求开始位置至结束位置的数据</p>
<pre><code>  String path = &quot;http://192.168.1.102:8080/editplus.exe&quot;;

  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setReadTimeout(5000);
  conn.setConnectTimeout(5000);
  conn.setRequestMethod(&quot;GET&quot;);
  
  //向服务器请求部分数据
  conn.setRequestProperty(&quot;Range&quot;, &quot;bytes=&quot; + startIndex + &quot;-&quot; + endIndex);
  conn.connect();
</code></pre>
</li>
<li><p>下载请求到的数据，存放至临时文件中</p>
<pre><code>  if(conn.getResponseCode() == 206)&#123;
      InputStream is = conn.getInputStream();
      RandomAccessFile raf = new RandomAccessFile(getFileName(path), &quot;rwd&quot;);
      //指定从哪个位置开始存放数据
      raf.seek(startIndex);
      byte[] b = new byte[1024];
      int len;
      while((len = is.read(b)) != -1)&#123;
          raf.write(b, 0, len);
      &#125;
      raf.close();
  &#125;
</code></pre>
<p>###多线程断点续传java版示例：</p>
<p>  public class Demo {</p>
<pre><code>  //定义要下载资源的网址(下载一个软件的地址)
  static String address = &quot;http://192.168.15.35:8080/PowerWord.100.exe&quot;;
  //定义开启的线程的个数
  static int threadCount = 3;
  //定义已完成下载的子线程个数
  static int finishedThreadCount = 0;
  
  public static void main(String[] args) &#123;
      try &#123;
          //get发送请求
          //1 封装URL
          URL url = new URL(address);
          //2 获取连接对象
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          //3 设置参数
          conn.setRequestMethod(&quot;GET&quot;);
          conn.setConnectTimeout(8000);
          conn.setReadTimeout(8000);
          //4 发送请求(可不写)
          
          //5 获取响应码，进行判断
          if (conn.getResponseCode()==200) &#123;
              
              //拿到要下载的文件的总长度
              int length = conn.getContentLength();
              //(封装临时文件)在本地自动生成一个临时文件(若存在，则不用生成)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)
              //第二个参数de作用是:可以立即将读取的数据写入临时文件
              //路径是：工作空间/工程目录/
              RandomAccessFile raf = new RandomAccessFile(_getFileName(address), &quot;rwd&quot;); 
              
              //设置临时文件与下载文件大小一致
              raf.setLength(length);
              //关闭临时文件(就相当于关闭临时文件的输出流)
              raf.close();
              
              //计算每条线程要下载的长度
              int size = length/threadCount;
              //计算每条线程的开始位置与结束位置
              for (int i = 0; i &lt; threadCount; i++) &#123;
                  int startIndex = i * size;//开始位置
                  int endIndex = (i+1)*size-1;//结束位置
                  if (i==threadCount-1) &#123;
                      endIndex=length-1;
                  &#125;
                  //开启每一条线程
                  new DownloadThread(startIndex,endIndex,i).start();
              &#125;
          &#125;
      &#125; catch (Exception e) &#123;
          e.printStackTrace();
      &#125;
  &#125;
  //通过网址，获取文件名
  static String _getFileName(String path)&#123;
      int index = path.lastIndexOf(&quot;/&quot;);
      return path.substring(index + 1);
  &#125;
  
</code></pre>
<p>  }</p>
<p>  //自定义多线程:用于下载数据<br>  class DownloadThread extends Thread{</p>
<pre><code>  //定义成员变量，某线程开始下载处的角标，结束下载处的角标，线程的id
  int startIndex;
  int endIndex;
  int threadId;
  //构造函数
  public DownloadThread(int startIndex, int endIndex, int threadId) &#123;
      super();
      this.startIndex = startIndex;
      this.endIndex = endIndex;
      this.threadId = threadId;
  &#125;
  
  @Override
  public void run() &#123;
      //开启子线程下载目标文件
      try &#123;
          //将每个线程记录下载进度的临时文件进行封装
          File file = new File(threadId+&quot;.txt&quot;);
          //定义每个线程下载的进度lastTotal(初始化为0)
          int lastTotal=0;
          //判断每个线程记录下载进度的临时文件是否存在
          if (file.exists()) &#123;
              //获取输入流
              InputStream is = new FileInputStream(file);
              BufferedReader br = new BufferedReader(new InputStreamReader(is));
              //读取流数据，获取每个线程上一次的下载进度
              lastTotal = Integer.parseInt(br.readLine());
              //改变下载的开始位置，已经下载过的数据，就不要再去请求了
              startIndex += lastTotal;
              //关闭流
              is.close();
          &#125;
          
          System.out.println(&quot;线程&quot; + threadId + &quot;的最终下载区间为：&quot; + startIndex + &quot;~&quot; + endIndex);
          
          //GET请求下载数据 封装URl 获取连接对象 设置参数
          URL url = new URL(Demo.address);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod(&quot;GET&quot;);
          conn.setConnectTimeout(8000);
          conn.setReadTimeout(8000);
          //定义请求下载的数据的范围
          conn.setRequestProperty(&quot;Range&quot;,&quot;bytes=&quot;+startIndex+&quot;-&quot;+endIndex);
          
          //获取响应状态码，进行判断
          if (conn.getResponseCode()==206) &#123;
              
              //获取服务器返回的流，流里的数据只有startIndex到endIndex区间的数据，并不会包含目标文件所有数据
              InputStream is = conn.getInputStream();
              //定义一个字节数组，用来存储每次从流里读取的数据
              byte[] b = new byte[1024];
              //定义一个int变量，用来接收每次从流里读取数据的长度
              int len;
              //定义每个线程各自的总下载进度（初始化为lastTotal）
              int total = lastTotal;
              //(封装临时文件)每个子线程都有自己要下载的数据，在本地自动生成一个临时文件(若存在，则不用生成)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)
              RandomAccessFile raf = new RandomAccessFile(Demo._getFileName(Demo.address),&quot;rwd&quot;);
              //改变往raf中写入数据的开始位置
              raf.seek(startIndex);
              
              //开始往临时文件写数据
              //while循环写入
              while ((len=is.read(b))!=-1) &#123;
                  
                  //记录每条线程各自写入的总进度
                  total+=len;
                  
                  System.out.println(&quot;线程&quot; + threadId + &quot;已下载的字节数为:&quot; + total);
                  
                  //写入
                  raf.write(b,0,len);
                  
                  //为了完成断点续传，while每次循环,都要将下载的进度，写入一个文本临时文件中
                  //(封装此记录每条线程总进度的临时文件，)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)
                  RandomAccessFile rafProgress = new RandomAccessFile(file, &quot;rwd&quot;);
                  //写入线程进度
                  rafProgress.write((total+&quot;&quot;).getBytes());
                  //关闭临时文件(相当于关闭流)
                  rafProgress.close();
              &#125;
              System.out.println(&quot;线程&quot; + threadId + &quot;下载完毕---------------------&quot;);
              
              //此子线程的下载数据完成了，关闭此子线程临时文件(相当于关闭流)
              raf.close();
              //已完成下载的子线程个数+1
              Demo.finishedThreadCount++;
              //三个线程全部下载完毕，才去删除文本临时文件
              synchronized (Demo.address) &#123;
                  if (Demo.finishedThreadCount==3) &#123;
                      for (int i = 0; i &lt; Demo.threadCount; i++) &#123;
                          File f = new File(i+&quot;.txt&quot;);
                          if (f.exists()) &#123;
                              f.delete();
                          &#125;
                      &#125;
                      Demo.finishedThreadCount=0;
                  &#125;
              &#125;
          &#125;
      &#125; catch (Exception e) &#123;
          e.printStackTrace();
      &#125;
  &#125;
</code></pre>
<p>  }</p>
</li>
</ul>
<hr>
<p>#四、 带断点续传的多线程下载</p>
<blockquote>
<p>从上次下载结束的位置开始，继续下载</p>
</blockquote>
<ul>
<li><p>记录下载的当前进度，把该进度实时写入一个临时文件</p>
</li>
<li><p>下一次下载时，读取这个临时文件中的下载进度，也就是上一次下载的进度，从这个进度开始下载</p>
</li>
<li><p>定义一个int变量记录每条线程下载的数据总长度，然后加上该线程的下载开始位置，得到的结果就是下次下载时，该线程的开始位置，把得到的结果存入缓存文件</p>
<pre><code>  //用来记录当前线程总的下载长度
  int total = 0;
  while((len = is.read(b)) != -1)&#123;
      raf.write(b, 0, len);
      total += len;
      //每次下载都把新的下载位置写入缓存文本文件
      RandomAccessFile raf2 = new RandomAccessFile(threadId + &quot;.txt&quot;, &quot;rwd&quot;);
      raf2.write((startIndex + total + &quot;&quot;).getBytes());
      raf2.close();
  &#125;
</code></pre>
</li>
<li><p>下次下载开始时，先读取缓存文件中的值，得到的值就是该线程新的开始位置</p>
<pre><code>  FileInputStream fis = new FileInputStream(file);
  BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  String text = br.readLine();
  int newStartIndex = Integer.parseInt(text);
  //把读到的值作为新的开始位置
  startIndex = newStartIndex;
  fis.close();
</code></pre>
</li>
<li><p>三条线程都下载完毕之后，删除缓存文件</p>
<pre><code>  RUNNING_THREAD--;
  if(RUNNING_THREAD == 0)&#123;
      for(int i = 0; i &lt;= 3; i++)&#123;
          File f = new File(i + &quot;.txt&quot;);
          f.delete();
      &#125;
  &#125;
</code></pre>
</li>
</ul>
<hr>
<p>#手机版的断点续传多线程下载器</p>
<ul>
<li>把刚才的代码直接粘贴过来就能用，记得在访问文件时的路径要改成Android的目录，添加访问网络和外部存储的路径</li>
</ul>
<p>###手机版的断点续传多线程下载器示例</p>
<p> public class MainActivity extends Activity {<br>        //定义要下载资源的网址<br>        String address = “<a target="_blank" rel="noopener" href="http://192.168.1.105:8080/feiqiu.exe&quot;">http://192.168.1.105:8080/feiqiu.exe&quot;</a>;<br>        //定义开启的线程的个数<br>        int threadCount = 3;<br>        //定义已完成下载的子线程个数<br>        int finishedThreadCount = 0;<br>        //定义下载文件的总进度<br>        int currentPbProgress;</p>
<pre><code>    //查找控件
    private ProgressBar pb;
    private TextView tv;
    Handler handler = new Handler()&#123;
        public void handleMessage(android.os.Message msg) &#123;
            //更新文字显示下载进度
            tv.setText((long)pb.getProgress()*100/pb.getMax()+&quot;%&quot;);
        &#125;
    &#125;;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) &#123;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化控件
        pb = (ProgressBar) findViewById(R.id.pb);
        tv = (TextView) findViewById(R.id.tv);
    &#125;

    public void click(View v)&#123;
        
        //开启多线程，下载软件
        Thread t = new Thread()&#123;
            @Override
            public void run() &#123;
                try &#123;
                    //get发送请求
                    //1 封装URL
                    URL url = new URL(address);
                    //2 获取连接对象
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    //3 设置参数
                    conn.setRequestMethod(&quot;GET&quot;);
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    //4 发送请求(可不写)
                    
                    //5 获取响应码，进行判断
                    if (conn.getResponseCode()==200) &#123;
                        
                        //拿到要下载的文件的总长度
                        int length = conn.getContentLength();
                        //(封装临时文件)在本地自动生成一个临时文件(若存在，则不用生成)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)（文件1）
                        //第二个参数de作用是:可以立即将读取的数据写入临时文件
                        //路径是：sd卡
                        File file = new File(Environment.getExternalStorageDirectory(),_getFileName(address));
                        RandomAccessFile raf = new RandomAccessFile(file, &quot;rwd&quot;); 
                        
                        //设置临时文件与下载文件大小一致
                        raf.setLength(length);
                        //关闭临时文件(就相当于关闭临时文件的输出流)
                        raf.close();
                        
                        //设置进度条的总进度为目标文件总长
                        pb.setMax(length);
                        
                        //计算每条线程要下载的长度
                        int size = length/threadCount;
                        //计算每条线程的开始位置与结束位置
                        for (int i = 0; i &lt; threadCount; i++) &#123;
                            int startIndex = i * size;//开始位置
                            int endIndex = (i+1)*size-1;//结束位置
                            if (i==threadCount-1) &#123;
                                endIndex=length-1;
                            &#125;
                            //开启每一条线程
                            new DownloadThread(startIndex,endIndex,i).start();
                        &#125;
                    &#125;
                &#125; catch (Exception e) &#123;
                    e.printStackTrace();
                &#125;
            &#125;
        &#125;;t.start();
        
    &#125;
    
    //通过网址，获取文件名
    String _getFileName(String path)&#123;
        int index = path.lastIndexOf(&quot;/&quot;);
        return path.substring(index + 1);
    &#125;

    //自定义线程:用于下载数据
    class DownloadThread extends Thread&#123;
        //定义成员变量，某线程开始下载处的角标，结束下载处的角标，线程的id
        int startIndex;
        int endIndex;
        int threadId;
        //构造函数
        public DownloadThread(int startIndex, int endIndex, int threadId) &#123;
            super();
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.threadId = threadId;
        &#125;
        
        @Override
        public void run() &#123;
            //开启子线程下载目标文件
            try &#123;
                //将每个线程记录下载进度的临时文件进行封装（文件2）
                File fileProgess = new File(Environment.getExternalStorageDirectory(),threadId+&quot;.txt&quot;);
                //定义每个线程下载的进度lastTotal(初始化为0)
                int lastTotal=0;
                //判断每个线程记录下载进度的临时文件是否存在
                if (fileProgess.exists()) &#123;
                    //获取输入流
                    InputStream is = new FileInputStream(fileProgess);
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    //读取流数据，获取每个线程上一次的下载进度
                    lastTotal = Integer.parseInt(br.readLine());
                    //改变下载的开始位置，已经下载过的数据，就不要再去请求了
                    startIndex += lastTotal;
                    //关闭流
                    is.close();
                    
                    //把上一次下载的总进度写入进度条
                    currentPbProgress += lastTotal;
                    pb.setProgress(currentPbProgress);
                    handler.sendEmptyMessage(0);
                &#125;
                
                System.out.println(&quot;线程&quot; + threadId + &quot;的最终下载区间为：&quot; + startIndex + &quot;~&quot; + endIndex);
                
                //GET请求下载数据 封装URl 获取连接对象 设置参数
                URL url = new URL(address);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod(&quot;GET&quot;);
                conn.setConnectTimeout(8000);
                conn.setReadTimeout(8000);
                //定义请求下载的数据的范围
                conn.setRequestProperty(&quot;Range&quot;,&quot;bytes=&quot;+startIndex+&quot;-&quot;+endIndex);
                
                //获取响应状态码，进行判断
                if (conn.getResponseCode()==206) &#123;
                    
                    //获取服务器返回的流，流里的数据只有startIndex到endIndex区间的数据，并不会包含目标文件所有数据
                    InputStream is = conn.getInputStream();
                    //定义一个字节数组，用来存储每次从流里读取的数据
                    byte[] b = new byte[1024];
                    //定义一个int变量，用来接收每次从流里读取数据的长度
                    int len;
                    //定义每个线程各自的总下载进度（初始化为lastTotal）
                    int total = lastTotal;
                    //(封装临时文件)每个子线程都有自己要下载的数据，在本地自动生成一个临时文件(若存在，则不用生成)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)（文件3）
                    
                    File file = new File(Environment.getExternalStorageDirectory(),_getFileName(address));
                    RandomAccessFile raf = new RandomAccessFile(file, &quot;rwd&quot;); 
                    //改变往raf中写入数据的开始位置
                    raf.seek(startIndex);
                    
                    //开始往临时文件写数据
                    //while循环写入
                    while ((len=is.read(b))!=-1) &#123;
                        
                        //记录每条线程各自写入的总进度
                        total+=len;
                        
                        System.out.println(&quot;线程&quot; + threadId + &quot;已下载的字节数为:&quot; + total);
                        
                        //写入
                        raf.write(b,0,len);
                        
                        //为了完成断点续传，while每次循环,都要将下载的进度，写入一个文本临时文件中
                        //(封装此记录每条线程总进度的临时文件，)(另外封装RandomAccessFile文件对象相当于自动获取此文件的输出流)（文件4）
                        RandomAccessFile rafProgress = new RandomAccessFile(fileProgess, &quot;rwd&quot;);
                        //写入线程进度
                        rafProgress.write((total+&quot;&quot;).getBytes());
                        //关闭临时文件(相当于关闭流)
                        rafProgress.close();
                        
                        currentPbProgress += len;
                        //所有线程每次下载len个长度的字节，都会写到进度条的总进度中
                        pb.setProgress(currentPbProgress);
                        handler.sendEmptyMessage(0);
                    &#125;
                    System.out.println(&quot;线程&quot; + threadId + &quot;下载完毕---------------------&quot;);
                    
                    //此子线程的下载数据完成了，关闭此子线程临时文件(相当于关闭流)
                    raf.close();
                    //已完成下载的子线程个数+1
                    finishedThreadCount++;
                    //三个线程全部下载完毕，才去删除文本临时文件
                    synchronized (address) &#123;
                        if (finishedThreadCount==3) &#123;
                            for (int i = 0; i &lt; threadCount; i++) &#123;
                                //（文件5）
                                File f = new File(Environment.getExternalStorageDirectory(), i+&quot;.txt&quot;);
                                if (f.exists()) &#123;
                                    f.delete();
                                &#125;
                            &#125;
                            finishedThreadCount=0;
                        &#125;
                    &#125;
                &#125;
            &#125; catch (Exception e) &#123;
                e.printStackTrace();
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<p>###用进度条显示下载进度</p>
<ul>
<li><p>拿到下载文件总长度时，设置进度条的最大值</p>
<pre><code>  //设置进度条的最大值
  pb.setMax(length);
</code></pre>
</li>
<li><p>进度条需要显示三条线程的整体下载进度，所以三条线程每下载一次，就要把新下载的长度加入进度条</p>
<ul>
<li><p>定义一个int全局变量，记录三条线程的总下载长度</p>
<pre><code>  int progress;
</code></pre>
</li>
<li><p>刷新进度条</p>
<pre><code>  while((len = is.read(b)) != -1)&#123;
      raf.write(b, 0, len);

              
      //把当前线程本次下载的长度加到进度条里
      progress += len;
      pb.setProgress(progress);
</code></pre>
</li>
</ul>
</li>
<li><p>每次断点下载时，从新的开始位置开始下载，进度条也要从新的位置开始显示，在读取缓存文件获取新的下载开始位置时，也要处理进度条进度</p>
<pre><code>  FileInputStream fis = new FileInputStream(file);
  BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  String text = br.readLine();
  int newStartIndex = Integer.parseInt(text);
              
  //新开始位置减去原本的开始位置，得到已经下载的数据长度
  int alreadyDownload = newStartIndex - startIndex;
  //把已经下载的长度设置入进度条
  progress += alreadyDownload;
</code></pre>
<p>###添加文本框显示百分比进度</p>
<pre><code>  tv.setText(progress * 100 / pb.getMax() + &quot;%&quot;);
</code></pre>
</li>
</ul>
<hr>
<p>#HttpUtils框架的使用</p>
<blockquote>
<p>HttpUtils本身就支持多线程断点续传，使用起来非常的方便</p>
</blockquote>
<ul>
<li><p>创建HttpUtils对象</p>
<pre><code>  HttpUtils http = new HttpUtils();
</code></pre>
</li>
<li><p>下载文件</p>
<pre><code>  http.download(url, //下载请求的网址
          target, //下载的数据保存路径和文件名
          true, //是否开启断点续传
          true, //如果服务器响应头中包含了文件名，那么下载完毕后自动重命名
          new RequestCallBack&lt;File&gt;() &#123;//侦听下载状态
      
      //下载成功此方法调用
      @Override
      public void onSuccess(ResponseInfo&lt;File&gt; arg0) &#123;
          tv.setText(&quot;下载成功&quot; + arg0.result.getPath());
      &#125;
      
      //下载失败此方法调用，比如文件已经下载、没有网络权限、文件访问不到，方法传入一个字符串参数告知失败原因
      @Override
      public void onFailure(HttpException arg0, String arg1) &#123;
          tv.setText(&quot;下载失败&quot; + arg1);
      &#125;
      
      //在下载过程中不断的调用，用于刷新进度条
      @Override
      public void onLoading(long total, long current, boolean isUploading) &#123;
          super.onLoading(total, current, isUploading);
          //设置进度条总长度
          pb.setMax((int) total);
          //设置进度条当前进度
          pb.setProgress((int) current);
          tv_progress.setText(current * 100 / total + &quot;%&quot;);
      &#125;
  &#125;);
</code></pre>
</li>
</ul>
<p>###关联源码 在Android private libraries下的jar，不能通过点击按钮直接关联</p>
<ul>
<li>1.在libs下创建一个FIle，名字为jar包名.jar.propertise(如：xUtils-2.6.14.jar.properties)</li>
<li>2.在.properties中写入 src=源码路径(如 src=D:\android\android58\02Android\\Androidsrc\xUtils-master\xUtils-master\library\src)不允许中文</li>
<li>3.重启eclipse</li>
</ul>
<p>###HttpUtils框架的使用示例</p>
<pre><code>public class MainActivity extends Activity &#123;

    //定义要下载资源的网址
    String address = &quot;http://192.168.1.105:8080/feiqiu.exe&quot;;
    
    //查找控件
    private ProgressBar pb;
    private TextView tv_progress;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) &#123;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
                pb = (ProgressBar) findViewById(R.id.pb);
                tv_progress = (TextView) findViewById(R.id.tv_progress);
    &#125;
    
    //点击事件 使用xUtils框架完成下载
    public void click(View v)&#123;
        //创建xUtils框架对象
        HttpUtils utils = new HttpUtils();
        //调用方法实现下载 参数1-4： 1网址  2指定路径和文件名 开启断点续传 3如果响应头中有文件名，就使用响应头的文件名 4响应回调对象
        utils.download(address, &quot;/sdcard/feiqiu.exe&quot;, true, true, new RequestCallBack&lt;File&gt;() &#123;
            
            //下载完成后调用
            @Override
            public void onSuccess(ResponseInfo&lt;File&gt; responseInfo) &#123;
                //吐司 /sdcard/feiqiu.exe
                Toast.makeText(MainActivity.this, responseInfo.result.getPath(), 0).show();
            &#125;
            
            //下载失败时调用
            @Override
            public void onFailure(HttpException error, String msg) &#123;
                TextView tv_error = (TextView) findViewById(R.id.tv_error);
                tv_error.setText(msg);
            &#125;
            
            //下载过程中不断调用
            @Override
            public void onLoading(long total, long current, boolean isUploading) &#123;
                super.onLoading(total, current, isUploading);
                
                pb.setMax((int) total);
                pb.setProgress((int) current);
                tv_progress.setText(current*100/total+&quot;%&quot;);
            &#125;
        &#125;);
    &#125;
&#125;
</code></pre>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2014/10/13/%E5%9F%BA%E7%A1%80/%E5%9F%BA%E7%A1%80--%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0%E4%B8%8B%E8%BD%BD/" data-id="ckvc340qt00695dfyg6cw2hwf" data-title="Android基础--多线程断点续传下载" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Android%E5%9F%BA%E7%A1%80/" rel="tag">Android基础</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2014/10/13/%E5%9F%BA%E7%A1%80/%E5%9F%BA%E7%A1%80--Activity&%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC&%E6%95%B0%E6%8D%AE%E4%BC%A0%E9%80%92/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Android基础--Activity&amp;页面跳转&amp;数据传递
        
      </div>
    </a>
  
  
    <a href="/2014/10/13/%E5%9F%BA%E7%A1%80/%E5%9F%BA%E7%A1%80--%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Android基础--网络编程</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Activity/">Activity</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Studio%E4%BB%8B%E7%BB%8D/">Android Studio介绍</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android%E5%9F%BA%E7%A1%80/">Android基础</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android%E6%A8%A1%E6%9D%BF/">Android模板</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/BaseRecyclerViewAdapterHelper/">BaseRecyclerViewAdapterHelper</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Bug%E9%97%AE%E9%A2%98/">Bug问题</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Button/">Button</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/EditText/">EditText</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Fragment/">Fragment</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Gradle/">Gradle</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/ListView/">ListView</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/MD%E8%AF%AD%E6%B3%95/">MD语法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/MyEclipse/">MyEclipse</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/OSS/">OSS</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/RecyclerView/">RecyclerView</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SQL%E5%9F%BA%E7%A1%80/">SQL基础</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/UI%E6%8E%A7%E4%BB%B6/">UI控件</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/View/">View</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/ViewPager/">ViewPager</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/View%E7%AF%87/">View篇</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/as%E6%8A%A5%E9%94%99/">as报错</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/jar%E5%8C%85/">jar包</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/targetSdkVersion/">targetSdkVersion</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E4%B8%83%E7%89%9B%E4%BA%91/">七牛云</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E4%BA%8C%E7%BB%B4%E7%A0%81/">二维码</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%A0%E9%99%A4%E5%9B%BE%E7%89%87%E7%BC%93%E5%AD%98/">删除图片缓存</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8D%87%E7%BA%A7%E6%8A%A5%E9%94%99/">升级报错</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">博客搭建</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8E%BB%E6%8E%89v7%E5%8C%85/">去掉v7包</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%9B%BE%E7%89%87%E5%A4%84%E7%90%86/">图片处理</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%A4%9A%E5%AA%92%E4%BD%93/">多媒体</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%AE%9A%E5%88%B6%E7%B1%BB/">定制类</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B0%8F%E7%B1%B3%E6%89%8B%E6%9C%BA/">小米手机</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E6%92%AD%E6%9C%BA%E5%88%B6/">广播机制</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%BE%AE%E5%8D%9A%E5%88%86%E4%BA%AB/">微博分享</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%89%93%E5%8C%85APK%E6%8A%A5%E9%94%99/">打包APK报错</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%8C%81%E4%B9%85%E5%8C%96%E5%AD%98%E5%82%A8/">持久化存储</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%97%B6%E9%97%B4%E9%80%89%E6%8B%A9DatePicker/">时间选择DatePicker</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%9C%8D%E5%8A%A1/">服务</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%9D%83%E9%99%90/">权限</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%9E%84%E5%BB%BA%E5%9F%BA%E7%B1%BB/">构建基类</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8B%E8%AF%95/">测试</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%9B%91%E5%90%AC%E9%94%81%E5%B1%8F/">监听锁屏</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%B3%BB%E7%BB%9F%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC/">系统页面跳转</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%89%BA%E6%9C%AF%E6%8E%A2%E7%B4%A2/">艺术探索</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%AF%BB%E5%8F%96txt/">读取txt</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BE%93%E5%85%A5%E6%B3%95/">输入法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8/">音乐播放器</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E9%BB%91%E9%A9%AC%E5%8D%AB%E5%A3%AB/">黑马卫士</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Activity/" rel="tag">Activity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android-Studio/" rel="tag">Android Studio</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android-Studio%E4%BB%8B%E7%BB%8D/" rel="tag">Android Studio介绍</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android%E5%9F%BA%E7%A1%80/" rel="tag">Android基础</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android%E6%A8%A1%E6%9D%BF/" rel="tag">Android模板</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/BaseRecyclerViewAdapterHelper/" rel="tag">BaseRecyclerViewAdapterHelper</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Bug%E9%97%AE%E9%A2%98/" rel="tag">Bug问题</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Button/" rel="tag">Button</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/EditText/" rel="tag">EditText</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Fragment/" rel="tag">Fragment</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Gradle/" rel="tag">Gradle</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ListView/" rel="tag">ListView</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MD%E8%AF%AD%E6%B3%95/" rel="tag">MD语法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MyEclipse/" rel="tag">MyEclipse</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OSS/" rel="tag">OSS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RecyclerView/" rel="tag">RecyclerView</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SQL%E5%9F%BA%E7%A1%80/" rel="tag">SQL基础</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/UI%E6%8E%A7%E4%BB%B6/" rel="tag">UI控件</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/View/" rel="tag">View</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ViewPager/" rel="tag">ViewPager</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/View%E7%AF%87/" rel="tag">View篇</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/as%E6%8A%A5%E9%94%99/" rel="tag">as报错</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jar%E5%8C%85/" rel="tag">jar包</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/targetSdkVersion/" rel="tag">targetSdkVersion</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%83%E7%89%9B%E4%BA%91/" rel="tag">七牛云</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%87%E7%BA%A7%E6%8A%A5%E9%94%99/" rel="tag">升级报错</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9B%BE%E7%89%87%E5%A4%84%E7%90%86/" rel="tag">图片处理</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%A4%9A%E5%AA%92%E4%BD%93/" rel="tag">多媒体</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B7%A5%E4%BD%9C%E9%97%AE%E9%A2%98/" rel="tag">工作问题</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%BF%E6%92%AD%E6%9C%BA%E5%88%B6/" rel="tag">广播机制</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BA%94%E7%94%A8%E8%AF%A6%E6%83%85/" rel="tag">应用详情</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BE%AE%E5%8D%9A%E5%88%86%E4%BA%AB/" rel="tag">微博分享</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8C%81%E4%B9%85%E5%8C%96%E5%AD%98%E5%82%A8/" rel="tag">持久化存储</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%8D%E5%8A%A1/" rel="tag">服务</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9D%83%E9%99%90/" rel="tag">权限</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9E%84%E5%BB%BA%E5%9F%BA%E7%B1%BB/" rel="tag">构建基类</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%94%9F%E6%B4%BB/" rel="tag">生活</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BB%83%E6%89%8B/" rel="tag">练手</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%89%BA%E6%9C%AF%E6%8E%A2%E7%B4%A2/" rel="tag">艺术探索</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BE%93%E5%85%A5%E6%B3%95/" rel="tag">输入法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%AB%98%E7%BA%A7%E6%8A%80%E5%B7%A7/" rel="tag">高级技巧</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Activity/" style="font-size: 12.5px;">Activity</a> <a href="/tags/Android-Studio/" style="font-size: 10px;">Android Studio</a> <a href="/tags/Android-Studio%E4%BB%8B%E7%BB%8D/" style="font-size: 12.5px;">Android Studio介绍</a> <a href="/tags/Android%E5%9F%BA%E7%A1%80/" style="font-size: 20px;">Android基础</a> <a href="/tags/Android%E6%A8%A1%E6%9D%BF/" style="font-size: 10px;">Android模板</a> <a href="/tags/BaseRecyclerViewAdapterHelper/" style="font-size: 10px;">BaseRecyclerViewAdapterHelper</a> <a href="/tags/Bug%E9%97%AE%E9%A2%98/" style="font-size: 10px;">Bug问题</a> <a href="/tags/Button/" style="font-size: 10px;">Button</a> <a href="/tags/EditText/" style="font-size: 10px;">EditText</a> <a href="/tags/Fragment/" style="font-size: 18.75px;">Fragment</a> <a href="/tags/Gradle/" style="font-size: 10px;">Gradle</a> <a href="/tags/ListView/" style="font-size: 17.5px;">ListView</a> <a href="/tags/MD%E8%AF%AD%E6%B3%95/" style="font-size: 11.25px;">MD语法</a> <a href="/tags/MyEclipse/" style="font-size: 13.75px;">MyEclipse</a> <a href="/tags/OSS/" style="font-size: 11.25px;">OSS</a> <a href="/tags/RecyclerView/" style="font-size: 12.5px;">RecyclerView</a> <a href="/tags/SQL%E5%9F%BA%E7%A1%80/" style="font-size: 10px;">SQL基础</a> <a href="/tags/UI%E6%8E%A7%E4%BB%B6/" style="font-size: 15px;">UI控件</a> <a href="/tags/View/" style="font-size: 10px;">View</a> <a href="/tags/ViewPager/" style="font-size: 10px;">ViewPager</a> <a href="/tags/View%E7%AF%87/" style="font-size: 10px;">View篇</a> <a href="/tags/as%E6%8A%A5%E9%94%99/" style="font-size: 10px;">as报错</a> <a href="/tags/jar%E5%8C%85/" style="font-size: 10px;">jar包</a> <a href="/tags/targetSdkVersion/" style="font-size: 10px;">targetSdkVersion</a> <a href="/tags/%E4%B8%83%E7%89%9B%E4%BA%91/" style="font-size: 10px;">七牛云</a> <a href="/tags/%E5%8D%87%E7%BA%A7%E6%8A%A5%E9%94%99/" style="font-size: 10px;">升级报错</a> <a href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" style="font-size: 12.5px;">博客搭建</a> <a href="/tags/%E5%9B%BE%E7%89%87%E5%A4%84%E7%90%86/" style="font-size: 15px;">图片处理</a> <a href="/tags/%E5%A4%9A%E5%AA%92%E4%BD%93/" style="font-size: 12.5px;">多媒体</a> <a href="/tags/%E5%B7%A5%E4%BD%9C%E9%97%AE%E9%A2%98/" style="font-size: 16.25px;">工作问题</a> <a href="/tags/%E5%B9%BF%E6%92%AD%E6%9C%BA%E5%88%B6/" style="font-size: 12.5px;">广播机制</a> <a href="/tags/%E5%BA%94%E7%94%A8%E8%AF%A6%E6%83%85/" style="font-size: 10px;">应用详情</a> <a href="/tags/%E5%BE%AE%E5%8D%9A%E5%88%86%E4%BA%AB/" style="font-size: 10px;">微博分享</a> <a href="/tags/%E6%8C%81%E4%B9%85%E5%8C%96%E5%AD%98%E5%82%A8/" style="font-size: 13.75px;">持久化存储</a> <a href="/tags/%E6%9C%8D%E5%8A%A1/" style="font-size: 12.5px;">服务</a> <a href="/tags/%E6%9D%83%E9%99%90/" style="font-size: 10px;">权限</a> <a href="/tags/%E6%9E%84%E5%BB%BA%E5%9F%BA%E7%B1%BB/" style="font-size: 10px;">构建基类</a> <a href="/tags/%E7%94%9F%E6%B4%BB/" style="font-size: 11.25px;">生活</a> <a href="/tags/%E7%BB%83%E6%89%8B/" style="font-size: 11.25px;">练手</a> <a href="/tags/%E8%89%BA%E6%9C%AF%E6%8E%A2%E7%B4%A2/" style="font-size: 10px;">艺术探索</a> <a href="/tags/%E8%BE%93%E5%85%A5%E6%B3%95/" style="font-size: 10px;">输入法</a> <a href="/tags/%E9%AB%98%E7%BA%A7%E6%8A%80%E5%B7%A7/" style="font-size: 11.25px;">高级技巧</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">November 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">October 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">September 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">August 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">July 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">June 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">April 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">March 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">February 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">September 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/10/">October 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/04/">April 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/01/">January 2013</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/05/">May 2012</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/11/03/Android%20Studio/Github%E4%B8%8A%E6%8F%90%E4%BA%A4%E4%BB%A3%E7%A0%81/">Github上提交代码</a>
          </li>
        
          <li>
            <a href="/2017/10/31/RecyclerView/BaseRecyclerViewAdapterHelper%E5%BA%93%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/">BaseRecyclerViewAdapterHelper库的基本使用方法</a>
          </li>
        
          <li>
            <a href="/2017/10/31/%E5%9B%BE%E7%89%87%E5%A4%84%E7%90%86/%E4%B8%83%E7%89%9B%E4%BA%91--%E4%B8%83%E7%89%9B%E4%BA%91%E5%BF%AB%E9%80%9F%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87/">七牛云快速上传图片</a>
          </li>
        
          <li>
            <a href="/2017/10/31/Gradle/Gradle--Gradle%E6%B7%BB%E5%8A%A0%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93%E6%8A%A5%E5%BC%82%E5%B8%B8/">Gradle添加第三方库报异常</a>
          </li>
        
          <li>
            <a href="/2017/10/26/Android%20Studio/%E8%A7%A3%E5%86%B3Android-Studio-Gradle-%E9%94%99%E8%AF%AF-%E7%BC%96%E7%A0%81GBK%E7%9A%84%E4%B8%8D%E5%8F%AF%E6%98%A0%E5%B0%84%E5%AD%97%E7%AC%A6/">解决Android Studio Gradle 错误 编码GBK的不可映射字符</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 wxyass<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>